/* Created by Aquariuslt on 5/20/17.*/
verifyController.$inject = ['verifyService'];
function verifyController(verifyService) {
    var vm = this;
    vm.verifys = [];
    vm.untreatedList = [];
    vm.passList = [];
    vm.refuseList = [];

    //init index data
    vm.initAllVerify = function(){
        verifyService.getAllVerify(0, 0).then(function (data) {
            vm.verifys = data.data;
        });
    }

    //init untreated List
    vm.initUntreatedList = function(){
        verifyService.getAllVerify(0, 0).then(function(data){
            vm.untreatedList = data.data;
        });
    }

    //init pass List
    vm.initPassList = function(){
        verifyService.getAllVerify(1, 2).then(function(data){
            vm.passList = data.data;
        });
    }

    //init refuse List
    vm.initRefuseList = function(){
        verifyService.getAllVerify(2, 2).then(function(data){
            vm.refuseList = data.data;
        });
    }

    //block operation
    vm.modifyBlock = function($index){
        var selectVerify = vm.passList[$index];
        var block = (selectVerify.block==0)? 1 : 0;
        verifyService.modifyVerifyBlock(selectVerify._id, block).then(function(data){
            if(data.data.ok > 0){
                selectVerify.block = block;
                m_socket.emit("stateChange", selectVerify.mid, selectVerify);
                alert("block success");
            }else{
                alert("block failure");
            }
        });
    }

    //Event
    vm.passClick = function($index){
        var selectVerify = vm.verifys[$index];
        verifyService.modifyVerifyState(selectVerify._id, 1, "pass").then(function (data) {
            if(data.data.ok > 0){
                selectVerify.state = 1;
                vm.verifys.splice($index,1);
                vm.reason = "";
                m_socket.emit("stateChange", selectVerify.mid, selectVerify);
                alert("update success");
            }else{
                alert("update failure");
            }
        });
    }

    vm.refuseClick = function($index){
      var selectVerify = vm.verifys[$index];
      $("#id").val(selectVerify._id);
      $("#index").val($index);
      $("#modal-refuse").modal("show");
    }
    
    vm.refuseSubmit = function(){
        var id = $("#id").val();
        var reason = $("#reason").val();
        var index = $("#index").val();
        verifyService.modifyVerifyState(id, 2, reason).then(function (data) {
          if(data.data.ok > 0){
              var selectVerify = vm.verifys[index];
              selectVerify.msg = reason;
              m_socket.emit("stateChange", selectVerify.mid, selectVerify);
              vm.verifys.splice(index,1);
              $("#reason").val("");
              alert("update success");
              $("#modal-refuse").modal("hide");
          }else{
              alert("update failure");
          }
      });
    }
}

module.exports = verifyController;